package com.geostar.constructionland.analyse.service.adjust;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.geostar.constructionland.analyse.factory.AdjustAgrFactory;
import com.geostar.constructionland.analyse.mapper.AdjustVersionMapper;
import com.geostar.constructionland.common.entity.response.AdjustAgrVersion;
import com.geostar.constructionland.common.entity.response.LandInfo;
import com.geostar.constructionland.common.entity.response.model.AdjustModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yelll
 * @since 2020-6-2 14:14
 **/
@Service
@Slf4j
public class AdjustService {
    @Autowired
    AdjustVersionMapper adjustVersionMapper;
    public List<LandInfo> adjust(AdjustModel adjustModel) throws Exception{
        AdjustAgrVersion adjVersion = null;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("adjust_type",adjustModel.getAnalyseType());
        if(StringUtils.isBlank(adjustModel.getAdjustVersion())){
            queryWrapper.orderByDesc("adjust_version");
            List<AdjustAgrVersion> adjustAgrVersions =
                    adjustVersionMapper.selectList(queryWrapper);
            if(adjustAgrVersions.size() > 0){
                adjVersion = adjustAgrVersions.get(0);
            }
        }else{
            queryWrapper.eq("adjust_version",adjustModel.getAdjustVersion());
            adjVersion = adjustVersionMapper.selectOne(queryWrapper);
        }
        if(adjVersion==null){
            throw new Exception("没有相应的平差算法版本信息");
        }
        IAdjust adjustAgri = AdjustAgrFactory.getAdjustAgr(adjVersion,adjustModel);
        List<LandInfo> landInfos = adjustAgri.adjust(adjustModel);
        return landInfos;
    }

}
